#ifndef SCREENSHOT_H
#define SCREENSHOT_H

#include <QtCore/QObject>
#include <QPixmap>
#include <QWidget>

#include "../global/xnamespace.h"

class QLabel;
class QQmlEngine;
class QJSEngine;

X_BEGIN_NAMESPACE

class ScreenshotPrivate;

// 截图处理工具
class Screenshot : public QWidget
{
    Q_OBJECT

signals:
    void screenshotCompleted(QString filePath);

public:
    Screenshot(QWidget* parent = Q_NULLPTR);
    virtual ~Screenshot();

    static Screenshot &instance();
    static const char* qmlName();
    static QObject *qmlInstance(QQmlEngine *engine, QJSEngine *scriptEngine);

    Q_INVOKABLE void shootScreen(bool hideMainWindow);

protected:
    void resizeEvent(QResizeEvent *event) override;
    void keyPressEvent(QKeyEvent* event) override;
    void mousePressEvent(QMouseEvent* event) override;
    void mouseReleaseEvent(QMouseEvent* event) override;
    void mouseDoubleClickEvent(QMouseEvent* event) override;
    void mouseMoveEvent(QMouseEvent *event) override;

private slots:
    void apply();
    void saveScreenshot(const QString& filePath);
    void shootScreen();
    void housekeep();

private:
    static Screenshot* createInstance();

private:
    QPixmap renderPixmap(QRect& rect);
    void updateScreenshotLabel();

private:
    bool m_hideMainWindow = false;
    bool m_selecting = false;
    bool m_corping = false;

    float m_scaleRatio = 0.0f;

    QPoint m_startPoint;
    QPoint m_endPoint;

    QPixmap m_originalPixmap;
    QLabel *m_screenshotLabel;
};

X_END_NAMESPACE

#endif // SCREENSHOT_H
